package com.bridgeintelligent.tag.webserver.dim.mgmt.service.indexdim;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.indexdim.IndexDimModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.indexdim.IndexDimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.indexdim.IndexNameInfo;
import com.bridgeintelligent.tag.webserver.dim.query.api.service.IQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "系统索引相关接口")
@RestController
@RequestMapping("/api/indexInfo")
@AllArgsConstructor
@Validated
@Slf4j
public class IndexNameInfoController {

    private IIndexDimService indexDimService;

    private IQueryService queryService;

    @ApiOperation(value = "查询有效的索引列表")
    @GetMapping("/indexNameList")
    public IndexDimRep<List<IndexNameInfo>> indexList() {
        try {
            List<IndexNameInfo> indexList = indexDimService.indexNameList("0");
            return new IndexDimRep<>(indexList, BaseCode.SUCCESS,"查询成功");
        } catch (Exception e) {
            log.error("查询失败",e);
            return new IndexDimRep<>(null,-1,"查询失败");
        }
    }

    @ApiOperation(value = "查询索引标签配置信息列表")
    @GetMapping("/indexDimList")
    public IndexDimRep<List<IndexDimModel>> indexDimList() {
        try {
            List<IndexDimModel> indexList = indexDimService.IndexDimList();
            return new IndexDimRep<>(indexList, BaseCode.SUCCESS,"查询成功");
        } catch (Exception e) {
            log.error("查询失败",e);
            return new IndexDimRep<>(null,-1,"查询失败");
        }
    }

    @ApiOperation(value = "新建索引标签配置信息", notes = "索引相关接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "dimVO", value = "DimVO信息", required = true)})
    @PostMapping("/add")
    public IndexDimRep add(@RequestBody IndexDimModel model) {
        try {
            indexDimService.addIndexDimInfo(model);
            return new IndexDimRep<>( BaseCode.SUCCESS,"新增成功");
        } catch (Exception e) {
            log.error("新增失败",e);
            return new IndexDimRep<>(-1,"新增失败");
        }
    }

    @ApiOperation(value = "修改索引标签配置信息", notes = "索引相关接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "dimVO", value = "DimVO信息", required = true)})
    @PostMapping("/update")
    public IndexDimRep update(@RequestBody IndexDimModel model) {
        try {
            indexDimService.updateIndexDimInfo(model);
            return new IndexDimRep<>( BaseCode.SUCCESS,"修改成功");
        } catch (Exception e) {
            log.error("修改失败",e);
            return new IndexDimRep<>(-1,"修改失败");
        }
    }

    @ApiOperation("删除索引配置信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "索引ID")})
    @GetMapping("/delIndexDim")
    public IndexDimRep delIndexDim(Integer id) {
        try {
            indexDimService.delIndexDimInfo(id);
            queryService.refresh();
            return new IndexDimRep<>( BaseCode.SUCCESS,"删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return new IndexDimRep<>(-1,"删除失败");
        }
    }

    @ApiOperation("根据ID查询索引标签配置信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "索引ID")
    })
    @GetMapping("/findIndexDimById")
    public IndexDimRep findIndexDimById(Integer id) {
        try {
            IndexDimModel indexDim = indexDimService.findIndexDimById(id);
            return new IndexDimRep<>( indexDim,BaseCode.SUCCESS,"查询成功");
        } catch (Exception e) {
            log.error("查询失败",e);
            return new IndexDimRep<>(-1,"查询失败");
        }
    }

    @ApiOperation(value = "启用索引")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "索引ID")
    })
    @GetMapping("/upIndex")
    public IndexDimRep upIndex(Integer id) {
        try {
            indexDimService.updateUpIndexStatus(id);
            queryService.refresh();
            return new IndexDimRep<>( BaseCode.SUCCESS,"启动成功");
        } catch (Exception e) {
            log.error("启动失败",e);
            return new IndexDimRep<>(-1,"启动失败");
        }
    }

    @ApiOperation(value = "禁用索引")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "索引ID")
    })
    @GetMapping("/downIndex")
    public IndexDimRep downIndex(Integer id) {
        try {
            indexDimService.updateDownIndexStatus(id);
            return new IndexDimRep<>( BaseCode.SUCCESS,"禁用成功");
        } catch (Exception e) {
            log.error("禁用失败",e);
            return new IndexDimRep<>(-1,"禁用失败");
        }
    }

    @ApiOperation(value = "校验索引编号或者索引名称的唯一性")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "校验类型"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "索引ID"),
            @ApiImplicitParam(paramType = "query", name = "indexCode", value = "索引编号"),
            @ApiImplicitParam(paramType = "query", name = "indexName", value = "索引名称")
    })
    @GetMapping("/checkIndex")
    public IndexDimRep checkIndex(Integer type,String id,String indexCode,String indexName) {
        try {
            boolean flag = indexDimService.checkCodeOrName(type, id, indexCode, indexName);
            return new IndexDimRep<>( flag,BaseCode.SUCCESS,"校验成功");
        } catch (Exception e) {
            log.error("校验成功",e);
            return new IndexDimRep<>(-1,"校验成功");
        }
    }

    @ApiOperation(value = "人工修改索引配置状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "索引ID"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态0/1")
    })
    @GetMapping("/updateStatusById")
    public IndexDimRep updateStatusById(String id,String status) {
        try {
            indexDimService.updateStatusById(id,status);
            return new IndexDimRep<>( BaseCode.SUCCESS,"修改索引配置状态成功");
        } catch (Exception e) {
            log.error("修改索引配置状态失败",e);
            return new IndexDimRep<>(-1,"修改索引配置状态失败");
        }
    }
}
